package com.younion.security;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.apache.shiro.web.util.WebUtils;

public class YounionFormAuthenticationFilter extends FormAuthenticationFilter{

    public static final String DEFAULT_CAPTCHA_PARAM = "randomCode";
    
    public static final String DEFAULT_USERNAME_PARAM = "user.account";
    
    public static final String DEFAULT_PASSWORD_PARAM = "user.password";

    private String captchaParam = DEFAULT_CAPTCHA_PARAM;

    public String getCaptchaParam() {
    	return captchaParam;
    }
    
    public String getUsernameParam() {
    	return DEFAULT_USERNAME_PARAM;
    }
    
    public String getPasswordParam() {
    	return DEFAULT_PASSWORD_PARAM;
    }

	protected String getCaptcha(ServletRequest request) {
		  
	      return WebUtils.getCleanParam(request, getCaptchaParam());
	}
  
	protected String getUsername(ServletRequest request) {
	      return WebUtils.getCleanParam(request, getUsernameParam());
	      
	}
	
	protected String getPassword(ServletRequest request) {
		  
	      return WebUtils.getCleanParam(request, getPasswordParam());
	}
	
	protected AuthenticationToken createToken(ServletRequest request, ServletResponse response) {

	      String username = getUsername(request);
	
	      String password = getPassword(request);
	
	      String captcha = getCaptcha(request);
	
	      boolean rememberMe = isRememberMe(request);
	
	      String host = getHost(request);
	
	      return new CaptchaUsernamePasswordToken(username, password, rememberMe, host, captcha);

	}

	@Override
    protected void issueSuccessRedirect(ServletRequest request, ServletResponse response) throws Exception {
        WebUtils.issueRedirect(request, response, getSuccessUrl());
    }

}


